package com.hrzyit.whistle.cases.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.CaseIndexDeptInfoVo
import com.hrzyit.whistle.cases.injection.component.DaggerCaseCenterComponent
import com.hrzyit.whistle.cases.injection.module.CaseCenterModule
import com.hrzyit.whistle.cases.presenter.TransferSelectDeptPresenter
import com.hrzyit.whistle.cases.presenter.view.TransferSelectDeptView
import com.hrzyit.whistle.cases.ui.adapter.SelectDeptAdapter
import kotlinx.android.synthetic.main.activity_transfer_select_dept.*
import me.yokeyword.indexablerv.IndexableLayout

/**
 * 批转单位选择界面
 * @author YangWei
 * @date 2019/5/4
 */
class TransferSelectDeptActivity : BaseMvpActivity<TransferSelectDeptPresenter>(),
    TransferSelectDeptView {
    companion object {
        const val TAG = "TransferSelectDeptActivity"
    }

    private var disposeUserId:Long = 0L
    private var disposeDeptId:Long = 0L
    private lateinit var mAdapter: SelectDeptAdapter
    private var userId: Long = 0L
    private var caseId: Long = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transfer_select_dept)

        initView()

        initData()
    }

    /**
     * 初始化视图
     */
    private fun initView() {

//        transferSelectDeptHeaderBar.getRightView().onClick {
//            XPopup.Builder(this)
//                .asConfirm(
//                    "是否批转案件到"
//                    , ""
//                ) {
////                    mPresenter.saveCaseDisposeUser(caseId, userId, disposeDeptId, disposeUserId)
//                }.show()
//        }

        // 初始化联系人索引选择器
        val indexLayout = transferSelectDepIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(this))

        // setAdapter
        mAdapter = SelectDeptAdapter(this)
        indexLayout.setAdapter(mAdapter)

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // set Listener
        // View v, int originalPosition, int currentPosition, T entity)
        mAdapter.setOnItemContentClickListener { v, oPos, cPos, entity ->
            // 单位id、单位名称
            // 跳转到人员选择界面，输入参数单位id、单位名称
        }

    }

    /**
     * 初始化数据
     */
    private fun initData() {
        // 获取案件id
        this.caseId = intent.getLongExtra("caseId", 0L)
        // 当前操作人id
        this.userId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
        // 获取当前操作人所在单位可派遣人员列表
        mPresenter.selectCaseTransferDeptList(caseId, userId)
    }

    /**
     * Dagger2 注入
     */
    override fun injectComponent() {
        DaggerCaseCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .caseCenterModule(CaseCenterModule())
            .build()
            .inject(this)

        mPresenter.mView = this
    }

    // 数据接口回调
    override fun onGetSelectCaseTransferDeptListSuccess(result: MutableList<CaseIndexDeptInfoVo>) {
        result?.let {
            mAdapter.setDatas(result)
        }
    }
}
